namespace SimpleWebServices.Models
{
    using System;
    using System.Linq;
    using System.Xml;
    using System.Xml.Serialization;
    using System.Xml.XPath;
    using SimpleWebServices;
    using SimpleWebServices.Xml;
    using Xunit;

    public sealed class SiteMapPublisherFacts
    {
        [Fact]
        public void typedef()
        {
            Assert.True(new ObjectExpectations<SiteMapPublisher>()
                .DerivesFrom<object>()
                .IsConcreteClass()
                .IsSealed()
                .HasDefaultConstructor()
                .XmlRoot("publisher", "http://www.sitemaps.org/schemas/sitemap/0.9")
                .Implements<IXmlSerializerNamespaces>()
                .Result);
        }

        [Fact]
        public void deserialize()
        {
            SiteMapPublisher actual = "<publisher xmlns='http://www.sitemaps.org/schemas/sitemap/0.9' type='SimpleWebServices.Models.TestIPublishSiteMapUrls, SimpleWebServices.Mvc.Facts' />".Deserialize<SiteMapPublisher>();

            Assert.NotNull(actual);

            Assert.Equal<string>(typeof(TestIPublishSiteMapUrls).AssemblyQualifiedName, actual.Type.AssemblyQualifiedName);
        }

        [Fact]
        public void deserializeEmpty()
        {
            SiteMapPublisher actual = "<publisher xmlns='http://www.sitemaps.org/schemas/sitemap/0.9' />".Deserialize<SiteMapPublisher>();

            Assert.NotNull(actual);

            Assert.Null(actual.Type);
        }

        [Fact]
        public void serialize()
        {
            SiteMapPublisher obj = new SiteMapPublisher
            {
                Type = typeof(TestIPublishSiteMapUrls)
            };

            XPathNavigator navigator = obj.Serialize().CreateNavigator();
            XmlNamespaceManager namespaces = new XmlNamespaceManager(navigator.NameTable);
            namespaces.AddNamespace("s", "http://www.sitemaps.org/schemas/sitemap/0.9");

            Assert.True((bool)navigator.Evaluate("1=count(/s:publisher[@type='" + string.Concat(obj.Type.FullName, ", ", obj.Type.Assembly.GetName().Name) + "'])", namespaces));
        }

        [Fact]
        public void ctor()
        {
            Assert.NotNull(new SiteMapPublisher());
        }

        [Fact]
        public void prop_Type()
        {
            Assert.True(new PropertyExpectations(typeof(SiteMapPublisher).GetProperty("Type"))
                .XmlIgnore()
                .TypeIs<Type>()
                .Set(null as string)
                .Set(typeof(TestIPublishSiteMapUrls))
                .ArgumentOutOfRangeException(typeof(DateTime))
                .Result);
        }

        [Fact]
        public void prop_TypeValue()
        {
            Assert.True(new PropertyExpectations(typeof(SiteMapPublisher).GetProperty("TypeValue"))
                .XmlAttribute("type")
                .TypeIs<string>()
                .Set(null as string)
                .ArgumentOutOfRangeException(string.Empty)
                .Set(typeof(TestIPublishSiteMapUrls).AssemblyQualifiedName)
                .ArgumentOutOfRangeException(typeof(DateTime).AssemblyQualifiedName)
                .Result);
        }

        [Fact]
        public void prop_XmlNamespaceDeclarations()
        {
            Assert.True(new PropertyExpectations(typeof(SiteMapPublisher).GetProperty("XmlNamespaceDeclarations"))
                .XmlNamespaceDeclarations()
                .TypeIs<XmlSerializerNamespaces>()
                .DefaultValueIsNotNull()
                .Result);
        }

        [Fact]
        public void prop_XmlNamespaceDeclarations_get()
        {
            XmlSerializerNamespaces actual = new SiteMapPublisher().XmlNamespaceDeclarations;

            Assert.Equal<int>(1, actual.Count);

            XmlQualifiedName name = actual.ToArray().GetValue(0) as XmlQualifiedName;
            Assert.Equal<string>(string.Empty, name.Name);
            Assert.Equal<string>("http://www.sitemaps.org/schemas/sitemap/0.9", name.Namespace);
        }

        [Fact]
        public void op_Publish()
        {
            SiteMapPublisher obj = new SiteMapPublisher
            {
                Type = typeof(TestIPublishSiteMapUrls)
            };

            Assert.Equal<int>(1, obj.Publish().Count());
        }
    }
}